package com.flainsky.nirvanaplayer.webservice;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;

import com.flainsky.nirvanaplayer.BaseValue;
import com.flainsky.nirvanaplayer.MainActivity;
import com.flainsky.nirvanaplayer.coder.MovieObjectXML;
import com.flainsky.nirvanaplayer.lib.objects.Movie;

public class FlaWebservice {
	//return Movie List
	//type -> MainActivity.CommendType(enum) 0 movie 1 live 2 show 3 tv 4 news
	public final static String AREA_METHOD = "getAreas";					//参数1 int：type	
	public final static String CATEGORY_METHOD = "getCategory";				//参数1 int：type
	public final static String YEAR_METHOD = "getYears";					//参数1 int：type
	
	//return XML doc 
	public final static String ALL_METHOD = "getAll";
	public final static String ALL_MOVIE_METHOD = "getAllMovie";
	public final static String ALL_LIVE_METHOD = "getAllLive";
	
	//旧的服务接口，不推荐使用
	public final static String AYC_MOVIE_METHOD = "getMovieByAYC";			//参数3 Sting：地区名 string：年份string：种类
	public final static String AYC_LIVE_METHOD = "getLiveByAYC";			//参数3 Sting：地区名 string：年份string：种类
	
	public final static String MOVIE_NEWS_METHOD = "getMovieByNews";		//参数1 int：count
	public final static String MOVIE_POPULAR_METHOD = "getMovieByPopular";	//参数1 int：count
	public final static String MOVIE_TIMES_METHOD = "getMovieByTimes";		//参数1 int：count
	
	public final static String LIVE_NEWS_METHOD = "getLiveByNews";			//参数1 int：count
	public final static String LIVE_POPULAR_METHOD = "getLiveByPopular";	//参数1 int：count
	public final static String LIVE_TIMES_METHOD = "getLiveByTimes";		//参数1 int：count
	
	//2012-1-11新的接口，兼容所有种类
	public final static String AYC_TYPE_METHOD = "getTypeByAYC";			//参数4 Sting：地区名 string：年份string：种类  int type
	public final static String TYPE_NEWS_METHOD = "getTypeByNews";			//参数2 int：count int:type
	public final static String TYPE_POPULAR_METHOD = "getTypeByPopular";	//参数2 int：count int:type
	public final static String TYPE_TIMES_METHOD = "getTypeByTimes";		//参数2 int：count int:type
	
	private static FlaWebservice instance;
	private BaseValue bv;
	String URN;
    String address;
    private static Context mContext;
	
    public static FlaWebservice getInstance() {
		if(instance!=null){
			return instance;
		}
		else{
			makeInstance(mContext);
			return instance;
		}
	}
    
	private FlaWebservice()
	{
		bv = BaseValue.getInstance();
		URN = bv.getURN();
		address = bv.getServiceAddress();
	}
	
	public static synchronized void makeInstance(Context context){
		mContext = context;
		if(instance==null){
			instance=new FlaWebservice();
		}
	}
	
	private String getWebServiceReturn(String methodName,List<FlaParameter> paraList)
	{
		SoapObject rpc = new SoapObject(URN, methodName);
		if(paraList!=null)
		{
			int lenth = paraList.size();
			for(int i = 0;i < lenth; i++)
			{
				FlaParameter fla = paraList.get(i);
				rpc.addProperty(fla.paraName,fla.paraValue);
			}
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(rpc); 
        HttpTransportSE  ht = new HttpTransportSE(address);
        String q = "";
        try {
			ht.call(URN+"/"+methodName, envelope);
			SoapObject so = (SoapObject) envelope.bodyIn; 
			Object obj =  so.getProperty("return");
			q = obj.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassCastException e)
		{
			e.printStackTrace();
		}
		return q;
	}
	/* old webservice
	public List<String> getStringList(String method)
	{
		String str = getWebServiceReturn(method, null);
		if(str == null || str.equals(""))
		{
			return null;
		}
		else
		{
			List<String> list = new ArrayList<String>();
			String [] sArray = str.split(";");
			for(int i = 0;i<sArray.length;i++)
			{
				list.add(sArray[i]);
			}
			return list;
		}
	}
	
	public List<Movie> getMovieList(String method,List<FlaParameter> paraList)
	{
		String str = FlaWebservice.getInstance().getWebServiceReturn(method, paraList);
		List<Movie> list = MovieObjectSerializable.MovieDecode(str);
		return list;
	}*/
	
	public List<Movie> getMovieList(String method,List<FlaParameter> paraList)
	{
		String str = FlaWebservice.getInstance().getWebServiceReturn(method, paraList);
		List<Movie> list = MovieObjectXML.MovieDecode(str);
		return list;
	}
	public List<String> getStringList(String method,MainActivity.CommendType type)
	{
		List<FlaParameter> paras = new ArrayList<FlaParameter>();
		paras.add(new FlaParameter("type",String.valueOf(MainActivity.getTypeByCommenType(type))));
		String str = FlaWebservice.getInstance().getWebServiceReturn(method,paras);
		List<String> list = MovieObjectXML.StringDecode(str);
		return list;
	}
}
